package graphics.system;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import tools.Help;
import tools.Log;
public class TextureContainer {
	private static HashMap<String, Texture> textures;
	private static long texsize;
	private static final String folder = "resources/texture/";
	/**
	 * loads all needed textures
	 */
	public static void loadTextures() {
		textures = new HashMap<String, Texture>();
		/*
		 * addTexture("resources/texture/fontmap2.png"); addTexture("resources/texture/explosionmap.png"); addTexture("resources/texture/smoke.png"); addTexture("resources/texture/spark.png"); //addTexture("resources/texture/lightmap.png"); addTexture("resources/texture/implosionmap.png"); addTexture("resources/texture/box.png");
		 */
		/*
		 * String folder = "resources/texture/"; String fileList[] = getImagesInFolder(folder); for (String s : fileList) { addTexture(folder + s); } long texsize = 0; for (Map.Entry<String, Texture> e : textures.entrySet()) { texsize += e.getValue().height * e.getValue().width * 4 * 4; // widht*height // * // 4 // (RGBA) // * // 4byte } Log.g("Textures loaded: " + ((float) texsize / 1024.0f) / 1000.0f + " mb");
		 */
		addTexture(folder + "null.png");
	}
	private static void addTexture(String fname) {
		try {
			Texture tex = new Texture(fname);
			textures.put(tex.name, tex);
			Log.g("loaded texture: " + fname);
		} catch (IOException e) {
			Help.p("Error: Texture '" + fname + "' not found!");
		}
	}
	public static Texture getTexture(String name) {
		Texture tex = textures.get(name);
		if (tex == null) { // texture not loaded yet -> try loading
			if (!name.contains(".png"))
				name = name + ".png";
			try {
				tex = new Texture(folder + name);
				textures.put(tex.name, tex);
				texsize += tex.width * tex.height * 4;
				Log.g("Texture memory: " + ((float) texsize / 1024.0f / 1000.0f) + "mb");
			} catch (IOException e) {
				Log.g("Error: Texture '" + folder + name + "' not found!");
				tex = null;
			}
		}
		return tex;
	}
	@SuppressWarnings("unused")
	private static String[] getImagesInFolder(String foldername) {
		File dir = new File(foldername);
		String[] fileList = dir.list(new FilenameFilter() {
			public boolean accept(File d, String name) {
				return name.endsWith(".png");
			}
		});
		return fileList;
	}
}
